﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using BIUtils;
using Spider;
using Entities;
using Dror.Common.Data.NHibernate;
using Dror.Common.Data.Contracts;

namespace BazaartBI
{
    class Program
    {
        private static readonly string GoogleAccessToken = "AIzaSyD8-mbz0E4ygBupbrOSoJTQFLLgCXqN92o";
        private static readonly string BingAccessToken = "Ahz14iO9BbbihtA_A2BSFNGGgfxxwz5YttQEaZGc7Ci-OJiHrE7IWG6DBetx3Jkn";
        
       
        static int Skip = 100; //skip every 100 meters
        static int MaxHouseNumber = 1000; //skip every 100 meters

        static void Main(string[] args)
        {
            var sessionFactory = new SessionFactory();
            var repository = new NHibernateRepository(sessionFactory);
            //FetchPlacesFromFile(repository, @"C:\Projects\BazaartBI\Fetcher\Data\blogs.txt");
            var streets = GetStreets();
            FetchPlacesByAddress(repository, MaxHouseNumber, Skip, streets);
        }

        static void FetchPlacesFromFile(IRepository repository, string file)
        {
            var fileUtils = new FileUtils();
            foreach (var blog in fileUtils.GetRows(file))
            {
                var place = new PlaceInfo() { WebsiteUrl = blog};
                SetPlaceInfoForUrl(place);
                repository.BeginTransaction();
                repository.SaveOrUpdate(place);
                repository.CommitTransaction();
            }
        }

        static void FetchPlacesByAddress(IRepository repository, int maxHouseNumber, int skip, IEnumerable<string> streets)
        {
            var utils = new GeoTagUtils();
            var googlePlacesClient = new GooglePlacesClient(GoogleAccessToken);
            var accumlatedPlaces = new List<PlaceInfo>();
            foreach (var street in streets)
            {
                for (var housNumber = 1; housNumber < maxHouseNumber; housNumber += skip)
                {
                    var address = string.Format("{0}, {1} ave, manhattan , new york, usa", housNumber, street);
                    var coordinate = utils.GetGeocodeResponse(BingAccessToken, address);
                    var places = googlePlacesClient.FindFashionInCoordinate(coordinate);

                    foreach (var place in places)
                    {
                        if (repository.Query<PlaceInfo>().Any(p => p.Name.ToLower() == place.Name.ToLower()))
                            continue;
                        SetPlaceInfoForUrl(place);
                        repository.BeginTransaction();
                        repository.SaveOrUpdate(place);
                        repository.CommitTransaction();
                    }
                    accumlatedPlaces.AddRange(places);
                }
            }
        }

        static void SetPlaceInfoForUrl(PlaceInfo place)
        {
            var spider = new Spider.Spider();
            var fbUtils = new FbUtils();
            var pinterestScaper = new PinterestScraper();

            place.Name = SubstringToSize(place.Name);
            place.Reference = SubstringToSize(place.Reference);
            place.Phone = SubstringToSize(place.Phone);
            place.Address = SubstringToSize(place.Address);

            var webpageInfo = spider.FindPageInfo(place.WebsiteUrl);
            place.ContactUsPageUrl = SubstringToSize(webpageInfo.ConvertToAbsoulteUrl(webpageInfo.ContactUrl));
            place.AboutUsPageUrl = SubstringToSize(webpageInfo.ConvertToAbsoulteUrl(webpageInfo.AboutUrl));
            place.FacebookPageUrl = SubstringToSize(webpageInfo.FacebokUrl);
            place.Pinterest = SubstringToSize(webpageInfo.PinterestlUrl);
            place.DoesIncludePinItButton = webpageInfo.DoesIncludePinItButton;

            foreach (var email in spider.FindAllEmails(webpageInfo))
            {
                place.Emails.Add(email);
            }

            place.Emails = place.Emails.Distinct().ToList();

            var fbIndetifier = place.GetFacebookUniqueIdentifier();
            if (!string.IsNullOrEmpty(fbIndetifier))
            {
                var businessInfo = fbUtils.GetPageInfo(fbIndetifier);
                place.Likes = businessInfo.Likes;

                foreach (var email in businessInfo.Emails)
                {
                    place.Emails.Add(email);
                }

                place.Emails = place.Emails.Distinct().ToList();
            }

            foreach (var email in place.Emails)
            {
                var pinterestUserInf = pinterestScaper.GetUserInfo(email);
                if (pinterestUserInf != null)
                    place.AddUserInfo(pinterestUserInf);
            }
        }

        static string SubstringToSize(string text)
        {
            return
                string.IsNullOrEmpty(text) ? string.Empty :
                text.Length > 1000 ? text.Substring(0, 1000) : text;
        }

        static IEnumerable<string> GetStreets()
        {
            return new List<string>(){
                "North end",
                "west",
                "greenwitch",
                "West broadway",
                "church",
                "broadway",
                "Lafayette",
                "centre",
                "baker",
                "mulbery",
                "mott",
                "Elizabeth",
                "bowery",
                "chyrysite",
                "forsyth",
                "eldridge",
                "allen",
                "orchard",
                "ludlow",
                "essex",
                "norfolk",
                "suffolk",
                "clinton",
                "ridge",
                "FDR Drive",
                "1",
                "2",
                "3",
                "4",
                "5",
                "6",
                "7",
                "8",
                "9",
                "10",
                "11",
                "12"
            };
        }

    }
}
